﻿using ABI.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using VirtualDesktop.Interfaces;

namespace WindowsDesktop.API
{
    public enum Vervsions
    {
        None = 0,
        Win10 = 1,
        Win11 = 2,
        Win11_21H2 = 3,
        Win11_23H2 = 4,
        Win11_23H2_3085 = 5,
        Win11_24H2 = 6,
    }
    public partial class AppServer : IAppServer
    {
        private bool IsInit = false;
        private Dictionary<Vervsions, bool> VersionDic = new Dictionary<Vervsions, bool>();
        public AppServer()
        {
            Init();
        }
        private void Init()
        {
            if (!IsInit)
            {
                VersionDic[Vervsions.Win10] = Win10.VirtualDesktop.Desktop.Count > 0;
                VersionDic[Vervsions.Win11] = Win11.VirtualDesktop.Desktop.Count > 0;
                VersionDic[Vervsions.Win11_21H2] = Win11_21H2.VirtualDesktop.Desktop.Count > 0;
                VersionDic[Vervsions.Win11_23H2] = Win11_23H2.VirtualDesktop.Desktop.Count > 0;
                VersionDic[Vervsions.Win11_23H2_3085] = Win11_23H2_3085.VirtualDesktop.Desktop.Count > 0;
                VersionDic[Vervsions.Win11_24H2] = Win11_24H2.VirtualDesktop.Desktop.Count > 0;
            }
            IsInit = true;
        }

        #region 需要给客户端提供的功能
        public IDesktop[] GetDesktops()
        {
            var version = VersionDic.Where(v => v.Value).First();
            //if (version == null) return;
            switch (version.Key)
            {
                case Vervsions.Win10:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win10.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win10.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                case Vervsions.Win11:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win11.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win11.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                case Vervsions.Win11_21H2:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win11_21H2.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win11_21H2.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                case Vervsions.Win11_23H2:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win11_23H2.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win11_23H2.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                case Vervsions.Win11_23H2_3085:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win11_23H2_3085.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win11_23H2_3085.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                case Vervsions.Win11_24H2:
                    {
                        List<IDesktop> re = new List<IDesktop>();
                        for (int i = 0; i < Win11_24H2.VirtualDesktop.DesktopManager.GetDesktopCount(); i++)
                        {
                            re.Add(Win11_24H2.VirtualDesktop.Desktop.FromIndex(i));
                        }
                        return re.ToArray();
                    }
                default:
                    break;
            }
            return new IDesktop[] { };
        }

        public IDesktop Create()
        {
            var version = VersionDic.Where(v => v.Value).First();
            //if (version == null) return;
            switch (version.Key)
            {
                case Vervsions.Win10:
                    return Win10.VirtualDesktop.Desktop.Create();
                case Vervsions.Win11:
                    return Win11.VirtualDesktop.Desktop.Create();
                case Vervsions.Win11_21H2:
                    return Win11_21H2.VirtualDesktop.Desktop.Create();
                case Vervsions.Win11_23H2:
                    return Win11_23H2.VirtualDesktop.Desktop.Create();
                case Vervsions.Win11_23H2_3085:
                    return Win11_23H2_3085.VirtualDesktop.Desktop.Create();
                case Vervsions.Win11_24H2:
                    return Win11_24H2.VirtualDesktop.Desktop.Create();
                default:
                    break;
            }
            return null;
        }

        public IDesktop FromHwnd(IntPtr hwnd)
        {
            var version = VersionDic.Where(v => v.Value).First();
            //if (version == null) return;
            switch (version.Key)
            {
                case Vervsions.Win10:
                    return Win10.VirtualDesktop.Desktop.FromWindow(hwnd);
                case Vervsions.Win11:
                    return Win11.VirtualDesktop.Desktop.FromWindow(hwnd);
                case Vervsions.Win11_21H2:
                    return Win11_21H2.VirtualDesktop.Desktop.FromWindow(hwnd);
                case Vervsions.Win11_23H2:
                    return Win11_23H2.VirtualDesktop.Desktop.FromWindow(hwnd);
                case Vervsions.Win11_23H2_3085:
                    return Win11_23H2_3085.VirtualDesktop.Desktop.FromWindow(hwnd);
                case Vervsions.Win11_24H2:
                    return Win11_24H2.VirtualDesktop.Desktop.FromWindow(hwnd);
                default:
                    break;
            }
            return null;
        }

        public IDesktop GetCurrent()
        {
            var version = VersionDic.Where(v => v.Value).First();
            //if (version == null) return;
            switch (version.Key)
            {
                case Vervsions.Win10:
                    return Win10.VirtualDesktop.Desktop.Current;
                case Vervsions.Win11:
                    return Win11.VirtualDesktop.Desktop.Current;
                case Vervsions.Win11_21H2:
                    return Win11_21H2.VirtualDesktop.Desktop.Current;
                case Vervsions.Win11_23H2:
                    return Win11_23H2.VirtualDesktop.Desktop.Current;
                case Vervsions.Win11_23H2_3085:
                    return Win11_23H2_3085.VirtualDesktop.Desktop.Current;
                case Vervsions.Win11_24H2:
                    return Win11_24H2.VirtualDesktop.Desktop.Current;
                default:
                    throw new NotImplementedException();
            }
        }
        #endregion


    }
}
